package cn.bdqn.pro.service.impl;

import cn.bdqn.model.EUList;
import cn.bdqn.model.PageBean;
import cn.bdqn.model.R;
import cn.bdqn.pro.mapper.ProRouteMapper;
import cn.bdqn.pro.mapper.RouteMapper;
import cn.bdqn.pro.model.ProRoute;
import cn.bdqn.pro.model.ProRouteExample;
import cn.bdqn.pro.model.Project;
import cn.bdqn.pro.model.Route;
import cn.bdqn.pro.service.RouteService;
import cn.bdqn.pro.vomapper.ProjectVoMapper;
import cn.bdqn.service.BaseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gurilla
 * @create 2019-09-10 21:34
 */
@Service
public class RouteServiceImpl extends BaseService implements RouteService {
    @Resource
    private RouteMapper routeMapper;

    @Resource
    private ProjectVoMapper projectVoMapper;

    @Resource
    private ProRouteMapper proRouteMapper;

    @Override
    public EUList<ProRoute> list(PageBean pageBean, String pname, String rname, Boolean isover) {
        ProRouteExample example = new ProRouteExample();
        ProRouteExample.Criteria criteria = example.createCriteria();
        //       criteria.andEidEqualTo(getEid());   //从session来
        if(!StringUtils.isEmpty(pname) || !StringUtils.isEmpty(rname)){
            criteria.andPnameLike("%"+pname+"%");
            criteria.andRnameLike("%"+rname+"%");
            criteria.andIsoverEqualTo(isover);
        }
        PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        List<ProRoute> proRoutes = proRouteMapper.selectByExample(example);
        PageInfo<ProRoute> info=new PageInfo<>(proRoutes);
        return new EUList<>(info.getTotal(),info.getList());
    }

    @Override
    public R save(Route route) {
        if(route.getId()==null){
            route.setOpid(getUserid());
            routeMapper.insertSelective(route);
        }else{
            route.setOpid(getUserid());
            routeMapper.updateByPrimaryKeySelective(route);
        }
        return R.ok();
    }

    @Override
    public R getProject() {
        List<Project> projects = projectVoMapper.getProject();
        return R.ok(null, projects);
    }
}
